/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.acceptance.worker;

import static org.junit.Assert.assertFalse;

import org.junit.Before;
import org.junit.Test;
import org.ourgrid.acceptance.util.worker.Req_004_Util;
import org.ourgrid.acceptance.util.worker.Req_120_Util;
import org.ourgrid.common.interfaces.Worker;
import org.ourgrid.common.interfaces.WorkerExecutionServiceClient;
import org.ourgrid.common.interfaces.management.RemoteWorkerManagement;
import org.ourgrid.common.interfaces.management.WorkerManagement;
import org.ourgrid.common.interfaces.status.WorkerStatusProvider;
import org.ourgrid.reqtrace.ReqTest;
import org.ourgrid.worker.WorkerComponent;
import org.ourgrid.worker.WorkerConstants;
import org.ourgrid.worker.controller.WorkerManagementClientFailureController;

@ReqTest(reqs="REQ120")
public class Req_120_Test extends WorkerAcceptanceTestCase {
	
	private Req_004_Util req_004_Util = new Req_004_Util(getComponentContext());
	private Req_120_Util req_120_Util = new Req_120_Util(getComponentContext());
	
	@Before
	public void setUp() throws Exception {
		super.setUp();
		workerAcceptanceUtil.simulateAuthentication();
	}
	
	/**
	 * This test contains the following steps:
	 *
	 *  1. Create a Worker with the public key property set to "publicKey1";
	 *  2. Start a Worker with the correct public key "publicKey1";
	 *  3. Stop the Worker with the public key "wrongPublicKey" - 
	 *  Verify if the following warn message was logged:
	 *        1. An unknown entity tried to stop the Worker. 
	 *        Only the local modules can perform this operation. 
	 *        Unknown entity public key: [wrongPublicKey].
	 */
	@ReqTest(test=" AT-120.1", reqs="REQ120")
	@Test public void test_at_120_1_StopWorkerWithWrongPublicKey() throws Exception {
		//Start worker
		WorkerComponent component = req_004_Util.startWorker();
		
		//Stop worker
		req_120_Util.stopWorker(component, "wrongPublicKey");
	} 
	
	/**
	 * This test contains the following steps:
	 *
	 *  1. _Start a Worker
	 *  2. Stop the Worker with the correct public key;
	 *  3. Verify if the following info message was logged:
	 *        1. "Worker has been successfully shutdown."
	 *  4. Verify if the following remote objects are NOT bound:
	 *        1. WORKER
	 *        2. WORKER_EXECUTION_LISTENER
	 *        3. LOCAL_WORKER_MANAGEMENT
	 *        4. REMOTE_WORKER_MANAGEMENT
	 *        5. WORKER_MANAGEMENT_CLIENT_MONITOR
	 *        6. WORKER_STATUS
	 *  5. Verify if the ControlClient received the operation succeed message.
	 */
	@ReqTest(test=" AT-120.2", reqs="REQ120")
	@Test public void test_at_120_2_WorkerStopCommand() throws Exception {
		//Start worker
		WorkerComponent component = req_004_Util.startWorker();
		
		//Stop worker
		//Verify if the ControlClient received the operation succeed message
		req_120_Util.stopWorker(component);
		
		//Verify if objects were unbounded
		assertFalse(isBound(component, WorkerConstants.WORKER, Worker.class));
		assertFalse(isBound(component, WorkerConstants.WORKER_EXECUTION_SERVICE_OBJECT_NAME, 
				WorkerExecutionServiceClient.class));
		assertFalse(isBound(component, WorkerConstants.LOCAL_WORKER_MANAGEMENT, 
				WorkerManagement.class));
		assertFalse(isBound(component, WorkerConstants.REMOTE_WORKER_MANAGEMENT, 
				RemoteWorkerManagement.class));
		assertFalse(isBound(component, WorkerConstants.LOCAL_WORKER_MANAGEMENT_CLIENT_MONITOR, 
				WorkerManagementClientFailureController.class));
		assertFalse(isBound(component, WorkerConstants.WORKER_STATUS_PROVIDER, 
				WorkerStatusProvider.class));
	}
}